data <- read.csv("apology_study1.csv")

SDO.mat <- data[c(31:46)]
SDO.mat[c(9:16)] <- (8)- SDO.mat[c(9:16)]
SDO <- apply(SDO.mat[1:16], 1, mean)

#dependent variables
colonial.rule = scale(data$ap1)
confort.woman = scale(data$ap2)
massacre = scale(data$ap3)
war.apology = (colonial.rule + confort.woman + massacre) / 3

data <- cbind(data, SDO, colonial.rule, confort.woman, massacre, war.apology)

colonial.apology = data$colonial.rule * -1
confort.woman.apology = data$confort.woman * -1
massacre.apology = data$massacre * -1
Resistance.to.apologies = war.apology * -1

data <- cbind(data, colonial.apology, confort.woman.apology, massacre.apology, Resistance.to.apologies)

#Table2 Correlation Matrix
corr.mat <- cbind(data$age, data$gender, data$education, data$SDO, data$militarism, data$ideology, data$Knowledge, data$Resistance.to.apologies)
cor(corr.mat, use="pairwise.complete.obs")

corr.mat

#centering independent variable and moderator
Ideology.centered <- data$Ideology - mean(data$Ideology, na.rm = TRUE)
Knowledge.centered <- data$Knowledge - mean(data$Knowledge, na.rm = TRUE)

data2 <- cbind(data, Ideology.centered, Knowledge.centered)

#Table3 Moderation Effect of Political Knowledge on the Association between Conservatism and Resistance to Group Apologies
library("pequod")

Table3 <- lmres(Resistance.to.apologies ~ Age + Gender.male + Education + SDO + Militaism+ Ideology * Knowledge, centered = c("Ideology", "Knowledge"), data = data2)
summary(Table3)

#Table4 Moderation Effect of Political Knowledge on the Association between SDO and Resistance to Group Apologies
library("pequod")

Table4 <- lmres(Resistance.to.apologies ~  Age + Gender.male + Education + SDO + Militaism+ Ideology + SDO * Knowledge, centered = c("SDO", "Knowledge"), data = data2)
summary(Table4)

#Fig1 Interaction effect between conservative ideology and political knowledge.
library("pequod")

model <- lmres(Resistance.to.apologies ~ Age + Gender.male + Education + SDO + Militaism + Ideology * Knowledge, centered = c("Ideology", "Knowledge"), data = data2)
summary(model)

model_ss <- simpleSlope(model, pred = "Ideology", mod1 = "Knowledge")
summary(model_ss)

PlotSlope(model_ss, namex = "Conservatism", namey = "Resistance to apologies", limitx=c(-2,2), limity=c(-1.7,-1.1))


#Fig2 Changes in the marginal effects of political conservatism on resistance to apologies by political knowledge
library("interplot")

reg.center <- lm(Resistance.to.apologies ~ data2$Age + data2$Gender.male + data2$Education + data2$SDO + data2$Militaism  + Ideology.centered*Knowledge.centered)

int1 <- interplot(m = reg.center, var1 = "Ideology.centered", var2 = "Knowledge.centered") +
  labs(x = "Political Knowledge", y = "Effects of conservatism")
print(int1)

#Fig3 Determinant factors in resistance to apologies in connection with the three items.
library("coefplot")

colonial.rule.pl <- lm(colonial.apology ~ Age + Gender.male + Education + SDO + Militaism  + Ideology*Knowledge, data = data2)
comfort.woman.pl <- lm(confort.woman.apology ~ Age + Gender.male + Education + SDO + Militaism  + Ideology*Knowledge, data = data2)
massacre.pl <- lm(massacre.apology ~ Age + Gender.male + Education + SDO + Militaism  + Ideology*Knowledge, data = data2)
multiplot(colonial.rule.pl, comfort.woman.pl, massacre.pl,
          intercept = FALSE, color = "black", lwdOuter = 1, numberAngle = 0,plot.linetypes = TRUE, legend.position = "bottom",
          xlab = "Coefficients", ylab = "variables", title = "Resistance to apologies",
          newNames = c(Age = "Age",
                       Gender.male = "Gender_male",
                       Education = "Education",
                       SDO = "SDO",
                       Militaism = "Militarism",
                       Ideology= "Ideology",
                       Knowledge= "Knowledge"))

